<template>
  <div class="com-container" @dblclick="revertMap">
    <div class="com-chart" ref="map_ref"></div>
  </div>
</template>
<script>
import axios from 'axios'
import { getProvinceMapInfo } from '../utils/map_utils.js'
import { mapState } from 'vuex'
export default {
  data() {
    return {
      chartInstance: null,
      allData: null, // 服务器返回的数据
      mapData: {}, // 所获取的省份的地图矢量数据
    };
  },
  created() {
    this.$socket.registerCallBack("mapData", this.getData)
  },
  mounted() {
    this.initChart();
    // this.getData();
    this.$socket.send({
      action: 'getData',
      socketType: 'mapData',
      chartName: 'map',
      value: ''
    });
    window.addEventListener("resize", this.sceenAdapter);
    this.sceenAdapter();
  },
  destroyed() {
    window.removeEventListener("resize", this.sceenAdapter);
    this.$socket.unRegisterCallBack("mapData")
  },
  methods: {
    //初始化echartInstance对象
    async initChart() {
      this.chartInstance = this.$echarts.init(this.$refs.map_ref, this.theme);
      //获取中国地图矢量数据
      const res = await axios.get('http://localhost:8999/static/map/china.json')
      this.$echarts.registerMap('china', res.data)
      //对图表初始化配置的控制
      const initOption = {
        title: {
            text: '▎商家分布',
            left: 20,
            top: 20
        },
        geo: {
            type: 'map',
            map: 'china',
            top: '5%',
            bottom: '5%',
            itemStyle: {
                areaColor: '#2E72BF',
                borderColor: '#333'
            }
        },
        legend: {
            left: '5%', 
            bottom: '5%',
            orient: 'vertical'
        }
      };
      this.chartInstance.setOption(initOption);
      this.chartInstance.on('click', async arg => {
          const provinceInfo =  getProvinceMapInfo(arg.name)
          //判断是否缓存了该省份
          if(!this.mapData[provinceInfo.key]) {
              const {data: res} = await axios.get('http://localhost:8999' + provinceInfo.path)
              this.mapData[provinceInfo.key] = res
              this.$echarts.registerMap(provinceInfo.key, res)
          }
          const changeOption = {
              geo: {
                  map: provinceInfo.key
              }
          }
          this.chartInstance.setOption(changeOption)
      })
    },
    // 获取服务器的数据
    async getData(res) {
      // const { data: res } = await this.$http.get("map");
      // console.log(res);
      this.allData = res;
      this.updateChart();
    },
    // 更新图表
    updateChart() {
      //处理数据
      const legendArr = this.allData.map(item => {
          return item.name
      })
      const seriesArr = this.allData.map(item=>{{
          //一个类别的所有散点
          return {
              type: 'effectScatter',
              rippleEffect: {
                  scale: 5,
                  brushType: 'stroke'
              },
              name: item.name,
              data: item.children,
              coordinateSystem: 'geo'
          }
      }})
      const dataOption = {
        series: seriesArr,
        legend: {
            data: legendArr
        }
      };
      this.chartInstance.setOption(dataOption);
    },
    handleSelect(selectType) {
      this.choiceType = selectType;
      this.updateChart();
      this.showChoice = false;
    },
    //适应屏幕大小
    sceenAdapter() {
      const titleFontSize = (this.$refs.map_ref.offsetWidth / 100) * 3.6;
      //和分辨率相关的文字大小
      const adapterOption = {
        legend: {
          itemWidth: titleFontSize / 2,
          itemHeight: titleFontSize / 2,
          itemGap: titleFontSize / 2,
          textStyle: {
            fontSize: titleFontSize / 2,
          },
          title: {
              textStyle: {
                  fontSize: titleFontSize
              }
          }
        },
      };
      this.chartInstance.setOption(adapterOption);

      this.chartInstance.resize();
    },
    ///回到中国地图
    revertMap() {
        const revertOption = {
              geo: {
                  map: 'china'
              }
          }
          this.chartInstance.setOption(revertOption)
    }
  },
  computed: {
    ...mapState(['theme'])
  },
  watch: {
    theme() {
      // console.log('主题切换');
      this.chartInstance.dispose() //销毁当前的图表
      this.initChart()
      this.sceenAdapter()
      this.updateChart()

    }
  }
};
</script>
<style lang='less' scoped>
</style>